package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 新增
     * @param dto
     * @return
     */
    @Override
    public Result addCategory(CategoryDTO dto) {
        Category category = new Category();
        //拷贝实体，传递参数
        BeanUtils.copyProperties(dto,category);
        //补全参数 AOP

        // 从当前线程上获取当前用户id AOP

        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.insert(category);
        return Result.success();
    }

    /**
     * 条件分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @Override
    public Result<PageResult> queryByPage(Integer page, Integer pageSize, String name, Integer type) {
        //开启分页，设置参数
        PageHelper.startPage(page,pageSize);
        Page<Category> categoryPage = categoryMapper.queryByPage(page,pageSize,name,type);

        //封装数据
        PageResult pageResult = new PageResult();
        pageResult.setTotal(categoryPage.getTotal());
        pageResult.setRecords(categoryPage.getResult());

        return Result.success(pageResult);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        // 查询该分类下是否关联菜品，有就抛异常不能删除
        int count = 0;
        count = dishMapper.selectByCategoryId(id);
        if (count>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        // 查询该分类下是否关联菜品，有就抛异常不能删除
        count = setmealMapper.selectByCategoryId(id);
        if (count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        categoryMapper.deleteById(id);
        return Result.success();
    }


    /**
     * 修改分类
     * @param dto
     * @return
     */
    @Override
    public Result update(CategoryDTO dto) {
        Category category = new Category();
        //拷贝数据
        BeanUtils.copyProperties(dto,category);
        //补全数据 AOP
        categoryMapper.update(category);
        return Result.success();
    }

    /**
     * 查询指定分类下的所有数据
     * @param type
     * @return
     */
    @Override
    public Result queryCategoryList(Integer type) {
//        String KEY = "category:"+type;
//        //先从缓存中查
//        ListOperations<String, String> listOperations = redisTemplate.opsForList();
//        List<String> range = listOperations.range(KEY, 0, -1);
//        if (CollectionUtils.isNotEmpty(range)){
//            List<Category> categoryList = range.stream().map(x -> JSON.parseObject(x, Category.class)).collect(Collectors.toList());
//            return Result.success(categoryList);
//        }

        List<Category> categoryList = categoryMapper.queryCategoryList(type);

////        //添加到缓存中
//        List<String> categoryListString = categoryList.stream().map(x -> JSON.toJSONString(x)).collect(Collectors.toList());
//
//        listOperations.leftPushAll(KEY,categoryListString);
        return Result.success(categoryList);
    }
}
